package com.ruoyi.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.domain.SysArea;
import com.ruoyi.system.mapper.SysAreaMapper;
import com.ruoyi.system.vo.resp.SelectOptionVo;
import com.ruoyi.system.vo.resp.SysAreaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 地区信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-19
 */
@Service
public class SysAreaServiceImpl implements ISysAreaService 
{
    @Autowired
    private SysAreaMapper sysAreaMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询地区信息
     * 
     * @param id 地区信息主键
     * @return 地区信息
     */
    @Override
    public SysArea selectSysAreaById(Long id)
    {
        return sysAreaMapper.selectSysAreaById(id);
    }

    /**
     * 查询地区信息列表
     * 
     * @param sysArea 地区信息
     * @return 地区信息
     */
    @Override
    public List<SysArea> selectSysAreaList(SysArea sysArea)
    {
        return sysAreaMapper.selectSysAreaList(sysArea);
    }

    /**
     * 查询地区信息列表
     *
     * @param sysArea 地区信息
     * @return 地区信息
     */
    @Override
    public List<SelectOptionVo> selectSysAreas(SysArea sysArea)
    {
        List<SelectOptionVo> selectOptionVos = null;
        if (Objects.isNull(sysArea.getPid()))   return selectOptionVos;
        selectOptionVos = sysAreaMapper.selectSysAreas(sysArea);
        List<SelectOptionVo> provinceOptionVos = new ArrayList<>();
        selectOptionVos.stream().forEach(i -> {
            SelectOptionVo areaRespVO = new SelectOptionVo();
            areaRespVO.setValue(i.getValue());
            areaRespVO.setLabel(i.getLabel());
            provinceOptionVos.add(areaRespVO);
        });
        if (1 == sysArea.getPid()) {
            for (int i = 0; i < selectOptionVos.size(); i++) {
                SelectOptionVo areaRespVO = selectOptionVos.get(i);
                if (areaRespVO.getValue().equals(23)) {
                    Collections.swap(selectOptionVos, i, 0);
                    break;
                }
            }
        }
        return selectOptionVos;
    }


    /**
     * 新增地区信息
     * 
     * @param sysArea 地区信息
     * @return 结果
     */
    @Override
    public int insertSysArea(SysArea sysArea)
    {
        return sysAreaMapper.insertSysArea(sysArea);
    }

    /**
     * 修改地区信息
     * 
     * @param sysArea 地区信息
     * @return 结果
     */
    @Override
    public int updateSysArea(SysArea sysArea)
    {
        return sysAreaMapper.updateSysArea(sysArea);
    }

    /**
     * 批量删除地区信息
     * 
     * @param ids 需要删除的地区信息主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaByIds(Long[] ids)
    {
        return sysAreaMapper.deleteSysAreaByIds(ids);
    }

    /**
     * 删除地区信息信息
     * 
     * @param id 地区信息主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaById(Long id)
    {
        return sysAreaMapper.deleteSysAreaById(id);
    }

    @Override
    public List<SysAreaVo> getAreaList(Long pid) {

         LoginUser loginUser = SecurityUtils.getLoginUser();
         SysUser sysUser = loginUser.getSysUser();

        //只取3层
        List<Integer> levelList = new ArrayList<Integer>();
        levelList.add(2);
        levelList.add(3);
        levelList.add(4);
//        levelList.add(5);


         ArrayList<SysArea> sysAreas = new ArrayList<>();
//        for (int i = 0; i <= 5; i++) {
////            List<SysArea> areaList = sysAreaMapper.selectList(page,new LambdaQueryWrapper<SysArea>()
////                    .eq(SysArea::getStatus,1).eq(SysArea::getLevel,5).orderByAsc(SysArea::getName));
//            Page<SysArea> page = new Page<>(1,10000);
//            IPage<SysArea> iPage = sysAreaMapper.selectPage(page, new LambdaQueryWrapper<SysArea>()
//                            .select(SysArea::getId,SysArea::getName,SysArea::getLevel)
//                    .eq(SysArea::getStatus, 1)
//                    .eq(SysArea::getLevel, 5));
//             List<SysArea> records = iPage.getRecords();
//            sysAreas.addAll(records);
//        }
        List<SysArea> streetList = sysAreaMapper.selectList( new LambdaQueryWrapper<SysArea>()
                .select(SysArea::getId,SysArea::getName,SysArea::getPid)
//                .eq(Objects.nonNull(sysUser.getStreetId()),SysArea::getId,sysUser.getStreetId())
                .eq(SysArea::getStatus, 1)
                .eq(SysArea::getLevel, 5));
        final long currentTimeMillis1 = System.currentTimeMillis();


        List<SysArea> areaList = sysAreaMapper.selectList(new LambdaQueryWrapper<SysArea>()
                .select(SysArea::getId,SysArea::getName,SysArea::getPid)
//                .eq(Objects.nonNull(sysUser.getProvinceId()),SysArea::getId,sysUser.getProvinceId())
                .eq(SysArea::getStatus,1).and(wq->wq.in(SysArea::getLevel, levelList)));


        areaList.addAll(streetList);
        List<SysAreaVo> pubList = new ArrayList<SysAreaVo>();
        //后面层数
        List<SysAreaVo> subList = new ArrayList<SysAreaVo>();

        for(SysArea s : areaList){
            if(s.getPid()==1L){

                SysAreaVo vo = new SysAreaVo();
                vo.setId(s.getId());
                vo.setPid(s.getPid());
                vo.setName(s.getName());
                pubList.add(vo);
            }else{
                SysAreaVo vo = new SysAreaVo();
                vo.setId(s.getId());
                vo.setPid(s.getPid());
                vo.setName(s.getName());
                subList.add(vo);
            }
        }

        Map<Long,List<SysAreaVo>> map = new HashMap<Long,List<SysAreaVo>>();

        for(SysAreaVo s : subList){
            if(map.get(s.getPid())==null){


                List<SysAreaVo> voList = new ArrayList<SysAreaVo>();

                voList.add(s);
                map.put(s.getPid(),voList);
            }else{

                map.get(s.getPid()).add(s);
            }

        }

        for(SysAreaVo s : pubList){
            s.setChildren(this.getChildrenList(s.getId(),map));
        }
        final long endTime = System.currentTimeMillis();
        return pubList;

    }


    private List<SysAreaVo> getChildrenList(Long pid, Map<Long,List<SysAreaVo>> tempMap){

        if(tempMap.get(pid)!=null){
            for(SysAreaVo s : tempMap.get(pid)){
                s.setChildren(this.getChildrenList(s.getId(), tempMap));
            }
            return tempMap.get(pid);
        }
        return null;

    }

}
